
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/solvers/diophantine.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:21 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Diophantine &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="diophantine.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Discrete" href="../discrete.html" />
    <link rel="prev" title="Differential Geometry" href="../diffgeom.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../discrete.html" title="Discrete"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../diffgeom.html" title="Differential Geometry"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Diophantine</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="diophantine">
<span id="diophantine-docs"></span><h1>Diophantine<a class="headerlink" href="#diophantine" title="Permalink to this headline">¶</a></h1>
<section id="diophantine-equations">
<h2>Diophantine equations<a class="headerlink" href="#diophantine-equations" title="Permalink to this headline">¶</a></h2>
<p>The word “Diophantine” comes with the name Diophantus, a mathematician lived
in the great city of Alexandria sometime around 250 AD. Often referred to as
the “father of Algebra”, Diophantus in his famous work “Arithmetica”
presented 150 problems that marked the early beginnings of number theory, the
field of study about integers and their properties. Diophantine equations play
a central and an important part in number theory.</p>
<p>We call a “Diophantine equation” to an equation of the form,
<span class="math notranslate nohighlight">\(f(x_1, x_2, \ldots x_n) = 0\)</span> where <span class="math notranslate nohighlight">\(n \geq 2\)</span> and <span class="math notranslate nohighlight">\(x_1, x_2, \ldots x_n\)</span> are
integer variables. If we can find <span class="math notranslate nohighlight">\(n\)</span> integers <span class="math notranslate nohighlight">\(a_1, a_2, \ldots a_n\)</span> such that
<span class="math notranslate nohighlight">\(x_1 = a_1, x_2 = a_2, \ldots x_n = a_n\)</span> satisfies the above equation, we say
that the equation is solvable. You can read more about Diophantine equations in
<a class="footnote-reference brackets" href="#id5" id="id1">1</a> and <a class="footnote-reference brackets" href="#id6" id="id2">2</a>.</p>
<p>Currently, following five types of Diophantine equations can be solved using
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diophantine()</span></code></a> and other helper functions of
the Diophantine module.</p>
<ul class="simple">
<li><p>Linear Diophantine equations: <span class="math notranslate nohighlight">\(a_1x_1 + a_2x_2 + \ldots + a_nx_n = b\)</span>.</p></li>
<li><p>General binary quadratic equation: <span class="math notranslate nohighlight">\(ax^2 + bxy + cy^2 + dx + ey + f = 0\)</span></p></li>
<li><p>Homogeneous ternary quadratic equation: <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 + dxy + eyz + fzx = 0\)</span></p></li>
<li><p>Extended Pythagorean equation: <span class="math notranslate nohighlight">\(a_{1}x_{1}^2 + a_{2}x_{2}^2 + \ldots + a_{n}x_{n}^2 = a_{n+1}x_{n+1}^2\)</span></p></li>
<li><p>General sum of squares: <span class="math notranslate nohighlight">\(x_{1}^2 + x_{2}^2 + \ldots + x_{n}^2 = k\)</span></p></li>
</ul>
</section>
<section id="module-structure">
<h2>Module structure<a class="headerlink" href="#module-structure" title="Permalink to this headline">¶</a></h2>
<p>This module contains <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diophantine()</span></code></a> and
helper functions that are needed to solve certain Diophantine equations. It’s
structured in the following manner.</p>
<ul class="simple">
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diophantine()</span></code></a></p>
<ul>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a></p>
<ul>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.classify_diop" title="sympy.solvers.diophantine.diophantine.classify_diop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">classify_diop()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="sympy.solvers.diophantine.diophantine.diop_linear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_linear()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_quadratic" title="sympy.solvers.diophantine.diophantine.diop_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_quadratic()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_ternary_quadratic()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic_normal" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic_normal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_ternary_quadratic_normal()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_pythagorean()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_sum_of_squares()</span></code></a></p></li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_even_powers" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_even_powers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_sum_of_even_powers()</span></code></a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.merge_solution" title="sympy.solvers.diophantine.diophantine.merge_solution"><code class="xref py py-meth docutils literal notranslate"><span class="pre">merge_solution()</span></code></a></p></li>
</ul>
</li>
</ul>
<p>When an equation is given to <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diophantine()</span></code></a>,
it factors the equation(if possible) and solves the equation given by each
factor by calling <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a> separately.
Then all the results are combined using <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.merge_solution" title="sympy.solvers.diophantine.diophantine.merge_solution"><code class="xref py py-meth docutils literal notranslate"><span class="pre">merge_solution()</span></code></a>.</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a> internally uses
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.classify_diop" title="sympy.solvers.diophantine.diophantine.classify_diop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">classify_diop()</span></code></a>
to find the type of the equation(and some other details) given to it and then
calls the appropriate solver function based on the type returned. For example,
if <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.classify_diop" title="sympy.solvers.diophantine.diophantine.classify_diop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">classify_diop()</span></code></a> returned “linear” as the
type of the equation, then <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a>
calls <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="sympy.solvers.diophantine.diophantine.diop_linear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_linear()</span></code></a> to solve the equation.</p>
<p>Each of the functions, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="sympy.solvers.diophantine.diophantine.diop_linear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_linear()</span></code></a>,
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_quadratic" title="sympy.solvers.diophantine.diophantine.diop_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_quadratic()</span></code></a>,
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_ternary_quadratic()</span></code></a>,
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_pythagorean()</span></code></a>
and <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_sum_of_squares()</span></code></a> solves a
specific type of equations and the type can be easily guessed by it’s name.</p>
<p>Apart from these functions, there are a considerable number of other functions
in the “Diophantine Module” and all of them are listed under User functions
and Internal functions.</p>
</section>
<section id="tutorial">
<h2>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
<p>First, let’s import the highest API of the Diophantine module.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine</span> <span class="kn">import</span> <span class="n">diophantine</span>
</pre></div>
</div>
<p>Before we start solving the equations, we need to define the variables.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;x, y, z&quot;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Let’s start by solving the easiest type of Diophantine equations, i.e. linear
Diophantine equations. Let’s solve <span class="math notranslate nohighlight">\(2x + 3y = 5\)</span>. Note that although we
write the equation in the above form, when we input the equation to any of the
functions in Diophantine module, it needs to be in the form <span class="math notranslate nohighlight">\(eq = 0\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">{(3*t_0 - 5, 5 - 2*t_0)}</span>
</pre></div>
</div>
<p>Note that stepping one more level below the highest API, we can solve the very
same equation by calling <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_solve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">(3*t_0 - 5, 5 - 2*t_0)</span>
</pre></div>
</div>
<p>Note that it returns a tuple rather than a set.
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diophantine()</span></code></a> always return a set of tuples.
But <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a> may return a single tuple
or a set of tuples depending on the type of the equation given.</p>
<p>We can also solve this equation by calling <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="sympy.solvers.diophantine.diophantine.diop_linear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_linear()</span></code></a>,
which is what <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_solve()</span></code></a> calls internally.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_linear</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">(3*t_0 - 5, 5 - 2*t_0)</span>
</pre></div>
</div>
<p>If the given equation has no solutions then the outputs will look like below.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">set()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">(None, None)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">(None, None)</span>
</pre></div>
</div>
<p>Note that except for the highest level API, in case of no solutions, a tuple of
<span class="math notranslate nohighlight">\(None\)</span> are returned. Size of the tuple is the same as the number of variables.
Also, one can specifically set the parameter to be used in the solutions by
passing a customized parameter. Consider the following example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;m&quot;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
<span class="go">(3*m_0 - 5, 5 - 2*m_0)</span>
</pre></div>
</div>
<p>For linear Diophantine equations, the customized parameter is the prefix used
for each free variable in the solution. Consider the following example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">7</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
<span class="go">(m_0, m_0 + 5*m_1 - 14, m_0 + 3*m_1 - 7)</span>
</pre></div>
</div>
<p>In the solution above, m_0 and m_1 are independent free variables.</p>
<p>Please note that for the moment, users can set the parameter only for linear
Diophantine equations and binary quadratic equations.</p>
<p>Let’s try solving a binary quadratic equation which is an equation with two
variables and has a degree of two. Before trying to solve these equations, an
idea about various cases associated with the equation would help a lot. Please
refer <a class="footnote-reference brackets" href="#id7" id="id3">3</a> and <a class="footnote-reference brackets" href="#id8" id="id4">4</a> for detailed analysis of different cases and the nature
of the solutions. Let us define <span class="math notranslate nohighlight">\(\Delta = b^2 - 4ac\)</span> w.r.t. the binary quadratic
<span class="math notranslate nohighlight">\(ax^2 + bxy + cy^2 + dx + ey + f = 0\)</span>.</p>
<p>When <span class="math notranslate nohighlight">\(\Delta &lt; 0\)</span>, there are either no solutions or only a finite number of solutions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">8</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">{(2, 1), (5, 1)}</span>
</pre></div>
</div>
<p>In the above equation <span class="math notranslate nohighlight">\(\Delta = (-4)^2 - 4*1*8 = -16\)</span> and hence only a finite
number of solutions exist.</p>
<p>When <span class="math notranslate nohighlight">\(\Delta = 0\)</span> we might have either no solutions or parameterized solutions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">6</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">set()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">{(-2*t**2 - 7*t + 10, -t**2 - 3*t + 5)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>
<span class="go">{(t_0, -t_0), (t_0, 3 - t_0)}</span>
</pre></div>
</div>
<p>The most interesting case is when <span class="math notranslate nohighlight">\(\Delta &gt; 0\)</span> and it is not a perfect square.
In this case, the equation has either no solutions or an infinite number of
solutions. Consider the below cases where <span class="math notranslate nohighlight">\(\Delta = 8\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">set()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;n&quot;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span>  <span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x_1</span><span class="p">,</span> <span class="n">y_1</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x_2</span><span class="p">,</span> <span class="n">y_2</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x_n</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x_1</span> <span class="o">==</span> <span class="n">x_n</span> <span class="ow">or</span> <span class="n">x_2</span> <span class="o">==</span> <span class="n">x_n</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y_n</span> <span class="o">=</span> <span class="o">-</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">4</span> <span class="o">+</span> <span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">4</span> <span class="o">+</span> <span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y_1</span> <span class="o">==</span> <span class="n">y_n</span> <span class="ow">or</span> <span class="n">y_2</span> <span class="o">==</span> <span class="n">y_n</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Here <span class="math notranslate nohighlight">\(n\)</span> is an integer. Although x_n and y_n may not look like
integers, substituting in specific values for n (and simplifying) shows that they
are. For example consider the following example where we set n equal to 9.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">simplify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">x_n</span><span class="o">.</span><span class="n">subs</span><span class="p">({</span><span class="n">n</span><span class="p">:</span> <span class="mi">9</span><span class="p">}))</span>
<span class="go">-9369318</span>
</pre></div>
</div>
<p>Any binary quadratic of the form <span class="math notranslate nohighlight">\(ax^2 + bxy + cy^2 + dx + ey + f = 0\)</span> can be
transformed to an equivalent form <span class="math notranslate nohighlight">\(X^2 - DY^2 = N\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">find_DN</span><span class="p">,</span> <span class="n">diop_DN</span><span class="p">,</span> <span class="n">transformation_to_DN</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">find_DN</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">(5, 920)</span>
</pre></div>
</div>
<p>So, the above equation is equivalent to the equation <span class="math notranslate nohighlight">\(X^2 - 5Y^2 = 920\)</span> after
a linear transformation. If we want to find the linear transformation, we can
use <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.transformation_to_DN" title="sympy.solvers.diophantine.diophantine.transformation_to_DN"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transformation_to_DN()</span></code></a></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">transformation_to_DN</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p>Here A is a 2 X 2 matrix and B is a 2 X 1 matrix such that the transformation</p>
<div class="math notranslate nohighlight">
\[\begin{split}\begin{bmatrix} X\\Y \end{bmatrix} = A \begin{bmatrix} x\\y \end{bmatrix} + B\end{split}\]</div>
<p>gives the equation <span class="math notranslate nohighlight">\(X^2 -5Y^2 = 920\)</span>. Values of <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span> are as belows.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">Matrix([</span>
<span class="go">[1/10, 3/10],</span>
<span class="go">[   0,  1/5]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span>
<span class="go">Matrix([</span>
<span class="go">[  1/5],</span>
<span class="go">[-11/5]])</span>
</pre></div>
</div>
<p>We can solve an equation of the form <span class="math notranslate nohighlight">\(X^2 - DY^2 = N\)</span> by passing <span class="math notranslate nohighlight">\(D\)</span> and <span class="math notranslate nohighlight">\(N\)</span> to
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_DN" title="sympy.solvers.diophantine.diophantine.diop_DN"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_DN()</span></code></a></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diop_DN</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">920</span><span class="p">)</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>Unfortunately, our equation has no solution.</p>
<p>Now let’s turn to homogeneous ternary quadratic equations. These equations are
of the form <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 + dxy + eyz + fzx = 0\)</span>. These type of equations
either have infinitely many solutions or no solutions (except the obvious
solution (0, 0, 0))</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">5</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">6</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="go">{(0, 0, 0)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">5</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="go">{(-16*p**2 + 28*p*q + 20*q**2, 3*p**2 + 38*p*q - 25*q**2, 4*p**2 - 24*p*q + 68*q**2)}</span>
</pre></div>
</div>
<p>If you are only interested in a base solution rather than the parameterized
general solution (to be more precise, one of the general solutions), you can
use <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_ternary_quadratic()</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_ternary_quadratic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">5</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">7</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="go">(-4, 5, 1)</span>
</pre></div>
</div>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_ternary_quadratic()</span></code></a> first converts the
given equation to an equivalent equation of the form <span class="math notranslate nohighlight">\(w^2 = AX^2 + BY^2\)</span> and
then it uses <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.descent" title="sympy.solvers.diophantine.diophantine.descent"><code class="xref py py-meth docutils literal notranslate"><span class="pre">descent()</span></code></a> to solve the latter
equation. You can refer to the docs of
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.transformation_to_normal" title="sympy.solvers.diophantine.diophantine.transformation_to_normal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transformation_to_normal()</span></code></a> to find more on
this. The equation <span class="math notranslate nohighlight">\(w^2 = AX^2 + BY^2\)</span> can be solved more easily by using the
Aforementioned <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.descent" title="sympy.solvers.diophantine.diophantine.descent"><code class="xref py py-meth docutils literal notranslate"><span class="pre">descent()</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">descent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">descent</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># solves the equation w**2 = 3*Y**2 + Z**2</span>
<span class="go">(1, 0, 1)</span>
</pre></div>
</div>
<p>Here the solution tuple is in the order (w, Y, Z)</p>
<p>The extended Pythagorean equation,
<span class="math notranslate nohighlight">\(a_{1}x_{1}^2 + a_{2}x_{2}^2 + \ldots + a_{n}x_{n}^2 = a_{n+1}x_{n+1}^2\)</span> and the
general sum of squares equation, <span class="math notranslate nohighlight">\(x_{1}^2 + x_{2}^2 + \ldots + x_{n}^2 = k\)</span> can
also be solved using the Diophantine module.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">f</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="mi">9</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">16</span><span class="o">*</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">49</span><span class="o">*</span><span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">e</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">25</span><span class="o">*</span><span class="n">f</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">{(70*t1**2 + 70*t2**2 + 70*t3**2 + 70*t4**2 - 70*t5**2, 105*t1*t5, 420*t2*t5, 60*t3*t5, 210*t4*t5, 42*t1**2 + 42*t2**2 + 42*t3**2 + 42*t4**2 + 42*t5**2)}</span>
</pre></div>
</div>
<p>function <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_pythagorean()</span></code></a> can
also be called directly to solve the same equation. Either you can call
<a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_pythagorean()</span></code></a> or use the high
level API. For the general sum of squares, this is also true, but one advantage
of calling <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares"><code class="xref py py-meth docutils literal notranslate"><span class="pre">diop_general_sum_of_squares()</span></code></a> is that
you can control how many solutions are returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_general_sum_of_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">18</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span>
<span class="go">{(0, 0, 3, 3), (0, 1, 1, 4), (1, 2, 2, 3)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_general_sum_of_squares</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">{(0, 0, 3, 3), (1, 2, 2, 3)}</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.sum_of_squares" title="sympy.solvers.diophantine.diophantine.sum_of_squares"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sum_of_squares()</span></code></a> routine will
providean iterator that returns solutions and one may control whether
the solutions contain zeros or not (and the solutions not containing
zeros are returned first):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">sum_of_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sos</span> <span class="o">=</span> <span class="n">sum_of_squares</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">zeros</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">sos</span><span class="p">)</span>
<span class="go">(1, 2, 2, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">sos</span><span class="p">)</span>
<span class="go">(0, 0, 3, 3)</span>
</pre></div>
</div>
<p>Simple Eqyptian fractions can be found with the Diophantine module, too.
For example, here are the ways that one might represent 1/2 as a sum of two
unit fractions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Eq</span><span class="p">,</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">Eq</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="n">y</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">))</span>
<span class="go">{(-2, 1), (1, -2), (3, 6), (4, 4), (6, 3)}</span>
</pre></div>
</div>
<p>To get a more thorough understanding of the Diophantine module, please
refer to the following blog.</p>
<p><a class="reference external" href="http://thilinaatsympy.wordpress.com/">http://thilinaatsympy.wordpress.com/</a></p>
</section>
<section id="references">
<h2>References<a class="headerlink" href="#references" title="Permalink to this headline">¶</a></h2>
<dl class="footnote brackets">
<dt class="label" id="id5"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>Andreescu, Titu. Andrica, Dorin. Cucurezeanu, Ion. An Introduction to
Diophantine Equations</p>
</dd>
<dt class="label" id="id6"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt>
<dd><p>Diophantine Equation, Wolfram Mathworld, [online]. Available:
<a class="reference external" href="http://mathworld.wolfram.com/DiophantineEquation.html">http://mathworld.wolfram.com/DiophantineEquation.html</a></p>
</dd>
<dt class="label" id="id7"><span class="brackets"><a class="fn-backref" href="#id3">3</a></span></dt>
<dd><p>Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0,[online],
Available: <a class="reference external" href="http://www.alpertron.com.ar/METHODS.HTM">http://www.alpertron.com.ar/METHODS.HTM</a></p>
</dd>
<dt class="label" id="id8"><span class="brackets"><a class="fn-backref" href="#id4">4</a></span></dt>
<dd><p>Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online],
Available: <a class="reference external" href="https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf">https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf</a></p>
</dd>
</dl>
</section>
<section id="user-functions">
<h2>User Functions<a class="headerlink" href="#user-functions" title="Permalink to this headline">¶</a></h2>
<p>This functions is imported into the global namespace
with <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sympy</span> <span class="pre">import</span> <span class="pre">*</span></code>:</p>
<section id="id9">
<h3>diophantine<a class="headerlink" href="#id9" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diophantine">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diophantine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">syms</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">permute</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1271-L1532"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diophantine" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplify the solution procedure of diophantine equation <code class="docutils literal notranslate"><span class="pre">eq</span></code> by
converting it into a product of terms which should equal zero.</p>
<p class="rubric">Explanation</p>
<p>For example, when solving, <span class="math notranslate nohighlight">\(x^2 - y^2 = 0\)</span> this is treated as
<span class="math notranslate nohighlight">\((x + y)(x - y) = 0\)</span> and <span class="math notranslate nohighlight">\(x + y = 0\)</span> and <span class="math notranslate nohighlight">\(x - y = 0\)</span> are solved
independently and combined. Each term is solved by calling
<code class="docutils literal notranslate"><span class="pre">diop_solve()</span></code>. (Although it is possible to call <code class="docutils literal notranslate"><span class="pre">diop_solve()</span></code>
directly, one must be careful to pass an equation in the correct
form and to interpret the output correctly; <code class="docutils literal notranslate"><span class="pre">diophantine()</span></code> is
the public-facing function to use in general.)</p>
<p>Output of <code class="docutils literal notranslate"><span class="pre">diophantine()</span></code> is a set of tuples. The elements of the
tuple are the solutions for each variable in the equation and
are arranged according to the alphabetic ordering of the variables.
e.g. For an equation with two variables, <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, the first
element of the tuple is the solution for <span class="math notranslate nohighlight">\(a\)</span> and the second for <span class="math notranslate nohighlight">\(b\)</span>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diophantine(eq,</span> <span class="pre">t,</span> <span class="pre">syms)</span></code>: Solve the diophantine
equation <code class="docutils literal notranslate"><span class="pre">eq</span></code>.
<code class="docutils literal notranslate"><span class="pre">t</span></code> is the optional parameter to be used by <code class="docutils literal notranslate"><span class="pre">diop_solve()</span></code>.
<code class="docutils literal notranslate"><span class="pre">syms</span></code> is an optional list of symbols which determines the
order of the elements in the returned tuple.</p>
<p>By default, only the base solution is returned. If <code class="docutils literal notranslate"><span class="pre">permute</span></code> is set to
True then permutations of the base solution and/or permutations of the
signs of the values will be returned when applicable.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">{(t_0, -t_0), (t_0, t_0)}</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="p">))</span>
<span class="go">{(0, n1, n2), (t_0, t_1, 2*t_0 + 3*t_1)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diophantine</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="go">{(0, n1), (3*t_0 - 4, -t_0)}</span>
</pre></div>
</div>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">eq</span></code> should be an expression which is assumed to be zero.
<code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solution.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="sympy.solvers.diophantine.diophantine.diop_solve"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_solve</span></code></a>, <a class="reference internal" href="../utilities/iterables.html#sympy.utilities.iterables.permute_signs" title="sympy.utilities.iterables.permute_signs"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.utilities.iterables.permute_signs</span></code></a>, <a class="reference internal" href="../utilities/iterables.html#sympy.utilities.iterables.signed_permutations" title="sympy.utilities.iterables.signed_permutations"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.utilities.iterables.signed_permutations</span></code></a></p>
</div>
</dd></dl>

<p>And this function is imported with <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sympy.solvers.diophantine</span> <span class="pre">import</span> <span class="pre">*</span></code>:</p>
</section>
<section id="classify-diop">
<h3>classify_diop<a class="headerlink" href="#classify-diop" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.classify_diop">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">classify_diop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_dict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1661-L1686"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.classify_diop" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</section>
</section>
<section id="internal-functions">
<h2>Internal Functions<a class="headerlink" href="#internal-functions" title="Permalink to this headline">¶</a></h2>
<p>These functions are intended for internal use in the Diophantine module.</p>
<section id="diop-solve">
<h3>diop_solve<a class="headerlink" href="#diop-solve" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_solve">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_solve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1576-L1658"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_solve" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the diophantine equation <code class="docutils literal notranslate"><span class="pre">eq</span></code>.</p>
<p class="rubric">Explanation</p>
<p>Unlike <code class="docutils literal notranslate"><span class="pre">diophantine()</span></code>, factoring of <code class="docutils literal notranslate"><span class="pre">eq</span></code> is not attempted. Uses
<code class="docutils literal notranslate"><span class="pre">classify_diop()</span></code> to determine the type of the equation and calls
the appropriate solver function.</p>
<p>Use of <code class="docutils literal notranslate"><span class="pre">diophantine()</span></code> is recommended over other helper functions.
<code class="docutils literal notranslate"><span class="pre">diop_solve()</span></code> can return either a set or a tuple depending on the
nature of the equation.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_solve(eq,</span> <span class="pre">t)</span></code>: Solve diophantine equation, <code class="docutils literal notranslate"><span class="pre">eq</span></code> using <code class="docutils literal notranslate"><span class="pre">t</span></code>
as a parameter if needed.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">eq</span></code> should be an expression which is assumed to be zero.
<code class="docutils literal notranslate"><span class="pre">t</span></code> is a parameter to be used in the solution.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine</span> <span class="kn">import</span> <span class="n">diop_solve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">w</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">(3*t_0 - 5, 5 - 2*t_0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">(t_0, 8*t_0 + 4*t_1 + 5, 7*t_0 + 3*t_1 + 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="n">w</span> <span class="o">-</span> <span class="mi">6</span><span class="p">)</span>
<span class="go">(t_0, t_0 + t_1, 6*t_0 + 5*t_1 + 4*t_2 - 6, 5*t_0 + 4*t_1 + 3*t_2 - 6)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_solve</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">{(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diophantine" title="sympy.solvers.diophantine.diophantine.diophantine"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diophantine</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="diop-linear">
<h3>diop_linear<a class="headerlink" href="#diop-linear" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_linear">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_linear</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1731-L1786"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves linear diophantine equations.</p>
<p>A linear diophantine equation is an equation of the form <span class="math notranslate nohighlight">\(a_{1}x_{1} +
a_{2}x_{2} + .. + a_{n}x_{n} = 0\)</span> where <span class="math notranslate nohighlight">\(a_{1}, a_{2}, ..a_{n}\)</span> are
integer constants and <span class="math notranslate nohighlight">\(x_{1}, x_{2}, ..x_{n}\)</span> are integer variables.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_linear(eq)</span></code>: Returns a tuple containing solutions to the
diophantine equation <code class="docutils literal notranslate"><span class="pre">eq</span></code>. Values in the tuple is arranged in the same
order as the sorted variables.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">eq</span></code> is a linear diophantine equation which is assumed to be zero.
<code class="docutils literal notranslate"><span class="pre">param</span></code> is the parameter to be used in the solution.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_linear</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span> <span class="c1"># solves equation 2*x - 3*y - 5 == 0</span>
<span class="go">(3*t_0 - 5, 2*t_0 - 5)</span>
</pre></div>
</div>
<p>Here x = -3*t_0 - 5 and y = -2*t_0 - 5</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diop_linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">z</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">(t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_quadratic" title="sympy.solvers.diophantine.diophantine.diop_quadratic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_quadratic</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_ternary_quadratic</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_general_pythagorean</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_general_sum_of_squares</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="base-solution-linear">
<h3>base_solution_linear<a class="headerlink" href="#base-solution-linear" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.base_solution_linear">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">base_solution_linear</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">c</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">t</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1789-L1843"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.base_solution_linear" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the base solution for the linear equation, <span class="math notranslate nohighlight">\(ax + by = c\)</span>.</p>
<p class="rubric">Explanation</p>
<p>Used by <code class="docutils literal notranslate"><span class="pre">diop_linear()</span></code> to find the base solution of a linear
Diophantine equation. If <code class="docutils literal notranslate"><span class="pre">t</span></code> is given then the parametrized solution is
returned.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">base_solution_linear(c,</span> <span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">t)</span></code>: <code class="docutils literal notranslate"><span class="pre">a</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>, <code class="docutils literal notranslate"><span class="pre">c</span></code> are coefficients
in <span class="math notranslate nohighlight">\(ax + by = c\)</span> and <code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solution.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">base_solution_linear</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">t</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">base_solution_linear</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># equation 2*x + 3*y = 5</span>
<span class="go">(-5, 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">base_solution_linear</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span> <span class="c1"># equation 5*x + 7*y = 0</span>
<span class="go">(0, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">base_solution_linear</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="c1"># equation 2*x + 3*y = 5</span>
<span class="go">(3*t - 5, 5 - 2*t)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">base_solution_linear</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="c1"># equation 5*x + 7*y = 0</span>
<span class="go">(7*t, -5*t)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="diop-quadratic">
<h3>diop_quadratic<a class="headerlink" href="#diop-quadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_quadratic">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_quadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1891-L1941"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_quadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves quadratic diophantine equations.</p>
<p>i.e. equations of the form <span class="math notranslate nohighlight">\(Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0\)</span>. Returns a
set containing the tuples <span class="math notranslate nohighlight">\((x, y)\)</span> which contains the solutions. If there
are no solutions then <span class="math notranslate nohighlight">\((None, None)\)</span> is returned.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_quadratic(eq,</span> <span class="pre">param)</span></code>: <code class="docutils literal notranslate"><span class="pre">eq</span></code> is a quadratic binary diophantine
equation. <code class="docutils literal notranslate"><span class="pre">param</span></code> is used to indicate the parameter to be used in the
solution.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">eq</span></code> should be an expression which is assumed to be zero.
<code class="docutils literal notranslate"><span class="pre">param</span></code> is a parameter to be used in the solution.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">t</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_quadratic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_quadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="go">{(-1, -1)}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_linear" title="sympy.solvers.diophantine.diophantine.diop_linear"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_linear</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_ternary_quadratic</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_general_sum_of_squares</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="sympy.solvers.diophantine.diophantine.diop_general_pythagorean"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_general_pythagorean</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r709"><span class="brackets"><a class="fn-backref" href="#id10">R709</a></span></dt>
<dd><p>Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online],
Available: <a class="reference external" href="http://www.alpertron.com.ar/METHODS.HTM">http://www.alpertron.com.ar/METHODS.HTM</a></p>
</dd>
<dt class="label" id="r710"><span class="brackets"><a class="fn-backref" href="#id11">R710</a></span></dt>
<dd><p>Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online],
Available: <a class="reference external" href="https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf">https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-dn">
<h3>diop_DN<a class="headerlink" href="#diop-dn" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_DN">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_DN</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">D</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">N</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">t</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1957-L2153"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_DN" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the equation <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span>.</p>
<p class="rubric">Explanation</p>
<p>Mainly concerned with the case <span class="math notranslate nohighlight">\(D &gt; 0, D\)</span> is not a perfect square,
which is the same as the generalized Pell equation. The LMM
algorithm <a class="reference internal" href="#r711" id="id12"><span>[R711]</span></a> is used to solve this equation.</p>
<p>Returns one solution tuple, (<span class="math notranslate nohighlight">\(x, y)\)</span> for each class of the solutions.
Other solutions of the class can be constructed according to the
values of <code class="docutils literal notranslate"><span class="pre">D</span></code> and <code class="docutils literal notranslate"><span class="pre">N</span></code>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_DN(D,</span> <span class="pre">N,</span> <span class="pre">t)</span></code>: D and N are integers as in <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span> and
<code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solutions.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">D</span></code> and <code class="docutils literal notranslate"><span class="pre">N</span></code> correspond to D and N in the equation.
<code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solutions.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_DN</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_DN</span><span class="p">(</span><span class="mi">13</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">)</span> <span class="c1"># Solves equation x**2 - 13*y**2 = -4</span>
<span class="go">[(3, 1), (393, 109), (36, 10)]</span>
</pre></div>
</div>
<p>The output can be interpreted as follows: There are three fundamental
solutions to the equation <span class="math notranslate nohighlight">\(x^2 - 13y^2 = -4\)</span> given by (3, 1), (393, 109)
and (36, 10). Each tuple is in the form (x, y), i.e. solution (3, 1) means
that <span class="math notranslate nohighlight">\(x = 3\)</span> and <span class="math notranslate nohighlight">\(y = 1\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diop_DN</span><span class="p">(</span><span class="mi">986</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># Solves equation x**2 - 986*y**2 = 1</span>
<span class="go">[(49299, 1570)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.find_DN" title="sympy.solvers.diophantine.diophantine.find_DN"><code class="xref py py-obj docutils literal notranslate"><span class="pre">find_DN</span></code></a>, <a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_bf_DN" title="sympy.solvers.diophantine.diophantine.diop_bf_DN"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_bf_DN</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r711"><span class="brackets">R711</span><span class="fn-backref">(<a href="#id12">1</a>,<a href="#id13">2</a>)</span></dt>
<dd><p>Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
Robertson, July 31, 2004, Pages 16 - 17. [online], Available:
<a class="reference external" href="https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf">https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="cornacchia">
<h3>cornacchia<a class="headerlink" href="#cornacchia" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.cornacchia">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">cornacchia</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2251-L2314"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.cornacchia" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves <span class="math notranslate nohighlight">\(ax^2 + by^2 = m\)</span> where <span class="math notranslate nohighlight">\(\gcd(a, b) = 1 = gcd(a, m)\)</span> and <span class="math notranslate nohighlight">\(a, b &gt; 0\)</span>.</p>
<p class="rubric">Explanation</p>
<p>Uses the algorithm due to Cornacchia. The method only finds primitive
solutions, i.e. ones with <span class="math notranslate nohighlight">\(\gcd(x, y) = 1\)</span>. So this method can’t be used to
find the solutions of <span class="math notranslate nohighlight">\(x^2 + y^2 = 20\)</span> since the only solution to former is
<span class="math notranslate nohighlight">\((x, y) = (4, 2)\)</span> and it is not primitive. When <span class="math notranslate nohighlight">\(a = b\)</span>, only the
solutions with <span class="math notranslate nohighlight">\(x \leq y\)</span> are found. For more details, see the References.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">cornacchia</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cornacchia</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">35</span><span class="p">)</span> <span class="c1"># equation 2x**2 + 3y**2 = 35</span>
<span class="go">{(2, 3), (4, 1)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cornacchia</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="c1"># equation x**2 + y**2 = 25</span>
<span class="go">{(4, 3)}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../utilities/iterables.html#sympy.utilities.iterables.signed_permutations" title="sympy.utilities.iterables.signed_permutations"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.utilities.iterables.signed_permutations</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r712"><span class="brackets"><a class="fn-backref" href="#id14">R712</a></span></dt>
<dd><ol class="upperalpha simple">
<li><p>Nitaj, “L’algorithme de Cornacchia”</p></li>
</ol>
</dd>
<dt class="label" id="r713"><span class="brackets"><a class="fn-backref" href="#id15">R713</a></span></dt>
<dd><p>Solving the diophantine equation ax**2 + by**2 = m by Cornacchia’s
method, [online], Available:
<a class="reference external" href="http://www.numbertheory.org/php/cornacchia.html">http://www.numbertheory.org/php/cornacchia.html</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-bf-dn">
<h3>diop_bf_DN<a class="headerlink" href="#diop-bf-dn" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_bf_DN">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_bf_DN</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">D</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">N</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">t</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2380-L2468"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_bf_DN" title="Permalink to this definition">¶</a></dt>
<dd><p>Uses brute force to solve the equation, <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span>.</p>
<p class="rubric">Explanation</p>
<p>Mainly concerned with the generalized Pell equation which is the case when
<span class="math notranslate nohighlight">\(D &gt; 0, D\)</span> is not a perfect square. For more information on the case refer
<a class="reference internal" href="#r714" id="id16"><span>[R714]</span></a>. Let <span class="math notranslate nohighlight">\((t, u)\)</span> be the minimal positive solution of the equation
<span class="math notranslate nohighlight">\(x^2 - Dy^2 = 1\)</span>. Then this method requires
<span class="math notranslate nohighlight">\(\sqrt{\\frac{\mid N \mid (t \pm 1)}{2D}}\)</span> to be small.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_bf_DN(D,</span> <span class="pre">N,</span> <span class="pre">t)</span></code>: <code class="docutils literal notranslate"><span class="pre">D</span></code> and <code class="docutils literal notranslate"><span class="pre">N</span></code> are coefficients in
<span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span> and <code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solutions.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">D</span></code> and <code class="docutils literal notranslate"><span class="pre">N</span></code> correspond to D and N in the equation.
<code class="docutils literal notranslate"><span class="pre">t</span></code> is the parameter to be used in the solutions.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_bf_DN</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_bf_DN</span><span class="p">(</span><span class="mi">13</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">)</span>
<span class="go">[(3, 1), (-3, 1), (36, 10)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_bf_DN</span><span class="p">(</span><span class="mi">986</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">[(49299, 1570)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.diop_DN" title="sympy.solvers.diophantine.diophantine.diop_DN"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diop_DN</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r714"><span class="brackets">R714</span><span class="fn-backref">(<a href="#id16">1</a>,<a href="#id17">2</a>)</span></dt>
<dd><p>Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
Robertson, July 31, 2004, Page 15. <a class="reference external" href="https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf">https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="transformation-to-dn">
<h3>transformation_to_DN<a class="headerlink" href="#transformation-to-dn" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.transformation_to_DN">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">transformation_to_DN</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2555-L2635"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.transformation_to_DN" title="Permalink to this definition">¶</a></dt>
<dd><p>This function transforms general quadratic,
<span class="math notranslate nohighlight">\(ax^2 + bxy + cy^2 + dx + ey + f = 0\)</span>
to more easy to deal with <span class="math notranslate nohighlight">\(X^2 - DY^2 = N\)</span> form.</p>
<p class="rubric">Explanation</p>
<p>This is used to solve the general quadratic equation by transforming it to
the latter form. Refer <a class="reference internal" href="#r715" id="id18"><span>[R715]</span></a> for more detailed information on the
transformation. This function returns a tuple (A, B) where A is a 2 X 2
matrix and B is a 2 X 1 matrix such that,</p>
<p>Transpose([x y]) =  A * Transpose([X Y]) + B</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">transformation_to_DN(eq)</span></code>: where <code class="docutils literal notranslate"><span class="pre">eq</span></code> is the quadratic to be
transformed.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">transformation_to_DN</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">transformation_to_DN</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span>
<span class="go">Matrix([</span>
<span class="go">[1/26, 3/26],</span>
<span class="go">[   0, 1/13]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span>
<span class="go">Matrix([</span>
<span class="go">[-6/13],</span>
<span class="go">[-4/13]])</span>
</pre></div>
</div>
<p>A, B  returned are such that Transpose((x y)) =  A * Transpose((X Y)) + B.
Substituting these values for <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> and a bit of simplifying work
will give an equation of the form <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">X</span><span class="p">,</span> <span class="n">Y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Matrix</span><span class="p">,</span> <span class="n">simplify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="p">(</span><span class="n">A</span><span class="o">*</span><span class="n">Matrix</span><span class="p">([</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">])</span> <span class="o">+</span> <span class="n">B</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># Transformation for x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span>
<span class="go">X/26 + 3*Y/26 - 6/13</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="p">(</span><span class="n">A</span><span class="o">*</span><span class="n">Matrix</span><span class="p">([</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">])</span> <span class="o">+</span> <span class="n">B</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># Transformation for y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span>
<span class="go">Y/13 - 4/13</span>
</pre></div>
</div>
<p>Next we will substitute these formulas for <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> and do
<code class="docutils literal notranslate"><span class="pre">simplify()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">zip</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">))))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span>
<span class="go">X**2/676 - Y**2/52 + 17/13</span>
</pre></div>
</div>
<p>By multiplying the denominator appropriately, we can get a Pell equation
in the standard form.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">*</span> <span class="mi">676</span>
<span class="go">X**2 - 13*Y**2 + 884</span>
</pre></div>
</div>
<p>If only the final equation is needed, <code class="docutils literal notranslate"><span class="pre">find_DN()</span></code> can be used.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.find_DN" title="sympy.solvers.diophantine.diophantine.find_DN"><code class="xref py py-obj docutils literal notranslate"><span class="pre">find_DN</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r715"><span class="brackets">R715</span><span class="fn-backref">(<a href="#id18">1</a>,<a href="#id19">2</a>)</span></dt>
<dd><p>Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0,
John P.Robertson, May 8, 2003, Page 7 - 11.
<a class="reference external" href="https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf">https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="transformation-to-normal">
<h3>transformation_to_normal<a class="headerlink" href="#transformation-to-normal" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.transformation_to_normal">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">transformation_to_normal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2837-L2850"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.transformation_to_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the transformation Matrix that converts a general ternary
quadratic equation <code class="docutils literal notranslate"><span class="pre">eq</span></code> (<span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 + dxy + eyz + fxz\)</span>)
to a form without cross terms: <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 = 0\)</span>. This is
not used in solving ternary quadratics; it is only implemented for
the sake of completeness.</p>
</dd></dl>

</section>
<section id="find-dn">
<h3>find_DN<a class="headerlink" href="#find-dn" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.find_DN">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">find_DN</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2689-L2730"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.find_DN" title="Permalink to this definition">¶</a></dt>
<dd><p>This function returns a tuple, <span class="math notranslate nohighlight">\((D, N)\)</span> of the simplified form,
<span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span>, corresponding to the general quadratic,
<span class="math notranslate nohighlight">\(ax^2 + bxy + cy^2 + dx + ey + f = 0\)</span>.</p>
<p>Solving the general quadratic is then equivalent to solving the equation
<span class="math notranslate nohighlight">\(X^2 - DY^2 = N\)</span> and transforming the solutions by using the transformation
matrices returned by <code class="docutils literal notranslate"><span class="pre">transformation_to_DN()</span></code>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">find_DN(eq)</span></code>: where <code class="docutils literal notranslate"><span class="pre">eq</span></code> is the quadratic to be transformed.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">find_DN</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">find_DN</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">(13, -884)</span>
</pre></div>
</div>
<p>Interpretation of the output is that we get <span class="math notranslate nohighlight">\(X^2 -13Y^2 = -884\)</span> after
transforming <span class="math notranslate nohighlight">\(x^2 - 3xy - y^2 - 2y + 1\)</span> using the transformation returned
by <code class="docutils literal notranslate"><span class="pre">transformation_to_DN()</span></code>.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.transformation_to_DN" title="sympy.solvers.diophantine.diophantine.transformation_to_DN"><code class="xref py py-obj docutils literal notranslate"><span class="pre">transformation_to_DN</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r716"><span class="brackets"><a class="fn-backref" href="#id20">R716</a></span></dt>
<dd><p>Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0,
John P.Robertson, May 8, 2003, Page 7 - 11.
<a class="reference external" href="https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf">https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-ternary-quadratic">
<h3>diop_ternary_quadratic<a class="headerlink" href="#diop-ternary-quadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_ternary_quadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameterize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2778-L2826"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the general quadratic ternary form,
<span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0\)</span>.</p>
<p>Returns a tuple <span class="math notranslate nohighlight">\((x, y, z)\)</span> which is a base solution for the above
equation. If there are no solutions, <span class="math notranslate nohighlight">\((None, None, None)\)</span> is returned.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_ternary_quadratic(eq)</span></code>: Return a tuple containing a basic solution
to <code class="docutils literal notranslate"><span class="pre">eq</span></code>.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">eq</span></code> should be an homogeneous expression of degree two in three variables
and it is assumed to be zero.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_ternary_quadratic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(1, 0, 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(1, 0, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic</span><span class="p">(</span><span class="mi">45</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">7</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">8</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(28, 45, 105)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">49</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">13</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span><span class="mi">8</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>
<span class="go">(9, 1, 5)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="square-factor">
<h3>square_factor<a class="headerlink" href="#square-factor" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.square_factor">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">square_factor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3148-L3171"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.square_factor" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an integer <span class="math notranslate nohighlight">\(c\)</span> s.t. <span class="math notranslate nohighlight">\(a = c^2k, \ c,k \in Z\)</span>. Here <span class="math notranslate nohighlight">\(k\)</span> is square
free. <span class="math notranslate nohighlight">\(a\)</span> can be given as an integer or a dictionary of factors.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">square_factor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">square_factor</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">square_factor</span><span class="p">(</span><span class="o">-</span><span class="mi">36</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">square_factor</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">square_factor</span><span class="p">({</span><span class="mi">3</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>  <span class="c1"># -18</span>
<span class="go">3</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../ntheory.html#sympy.ntheory.factor_.core" title="sympy.ntheory.factor_.core"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.factor_.core</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="descent">
<h3>descent<a class="headerlink" href="#descent" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.descent">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">descent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3258-L3308"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.descent" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a non-trivial solution, (x, y, z), to <span class="math notranslate nohighlight">\(x^2 = Ay^2 + Bz^2\)</span>
using Lagrange’s descent method with lattice-reduction. <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span>
are assumed to be valid for such a solution to exist.</p>
<p>This is faster than the normal Lagrange’s descent algorithm because
the Gaussian reduction is used.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">descent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">descent</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># x**2 = 3*y**2 + z**2</span>
<span class="go">(1, 0, 1)</span>
</pre></div>
</div>
<p><span class="math notranslate nohighlight">\((x, y, z) = (1, 0, 1)\)</span> is a solution to the above equation.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">descent</span><span class="p">(</span><span class="mi">41</span><span class="p">,</span> <span class="o">-</span><span class="mi">113</span><span class="p">)</span>
<span class="go">(-16, -3, 1)</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r717"><span class="brackets"><a class="fn-backref" href="#id21">R717</a></span></dt>
<dd><p>Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
Mathematics of Computation, Volume 00, Number 0.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-general-pythagorean">
<h3>diop_general_pythagorean<a class="headerlink" href="#diop-general-pythagorean" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_general_pythagorean">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_general_pythagorean</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3437-L3469"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_general_pythagorean" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the general pythagorean equation,
<span class="math notranslate nohighlight">\(a_{1}^2x_{1}^2 + a_{2}^2x_{2}^2 + . . . + a_{n}^2x_{n}^2 - a_{n + 1}^2x_{n + 1}^2 = 0\)</span>.</p>
<p>Returns a tuple which contains a parametrized solution to the equation,
sorted in the same order as the input variables.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_general_pythagorean(eq,</span> <span class="pre">param)</span></code>: where <code class="docutils literal notranslate"><span class="pre">eq</span></code> is a general
pythagorean equation which is assumed to be zero and <code class="docutils literal notranslate"><span class="pre">param</span></code> is the base
parameter used to construct other parameters by subscripting.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_general_pythagorean</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_general_pythagorean</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(m1**2 + m2**2 - m3**2, 2*m1*m3, 2*m2*m3, m1**2 + m2**2 + m3**2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_general_pythagorean</span><span class="p">(</span><span class="mi">9</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">16</span><span class="o">*</span><span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">25</span><span class="o">*</span><span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(10*m1**2  + 10*m2**2  + 10*m3**2 - 10*m4**2, 15*m1**2  + 15*m2**2  + 15*m3**2  + 15*m4**2, 15*m1*m4, 12*m2*m4, 60*m3*m4)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="diop-general-sum-of-squares">
<h3>diop_general_sum_of_squares<a class="headerlink" href="#diop-general-sum-of-squares" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_general_sum_of_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3472-L3509"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the equation <span class="math notranslate nohighlight">\(x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0\)</span>.</p>
<p>Returns at most <code class="docutils literal notranslate"><span class="pre">limit</span></code> number of solutions.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">general_sum_of_squares(eq,</span> <span class="pre">limit)</span></code> : Here <code class="docutils literal notranslate"><span class="pre">eq</span></code> is an expression which
is assumed to be zero. Also, <code class="docutils literal notranslate"><span class="pre">eq</span></code> should be in the form,
<span class="math notranslate nohighlight">\(x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0\)</span>.</p>
<p class="rubric">Details</p>
<p>When <span class="math notranslate nohighlight">\(n = 3\)</span> if <span class="math notranslate nohighlight">\(k = 4^a(8m + 7)\)</span> for some <span class="math notranslate nohighlight">\(a, m \in Z\)</span> then there will be
no solutions. Refer <a class="reference internal" href="#r718" id="id22"><span>[R718]</span></a> for more details.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_general_sum_of_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_general_sum_of_squares</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2345</span><span class="p">)</span>
<span class="go">{(15, 22, 22, 24, 24)}</span>
</pre></div>
</div>
<p class="rubric">Reference</p>
<dl class="citation">
<dt class="label" id="r718"><span class="brackets"><a class="fn-backref" href="#id22">R718</a></span></dt>
<dd><p>Representing an integer as a sum of three squares, [online],
Available:
<a class="reference external" href="http://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares">http://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-general-sum-of-even-powers">
<h3>diop_general_sum_of_even_powers<a class="headerlink" href="#diop-general-sum-of-even-powers" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_general_sum_of_even_powers">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_general_sum_of_even_powers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3512-L3542"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_general_sum_of_even_powers" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the equation <span class="math notranslate nohighlight">\(x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0\)</span>
where <span class="math notranslate nohighlight">\(e\)</span> is an even, integer power.</p>
<p>Returns at most <code class="docutils literal notranslate"><span class="pre">limit</span></code> number of solutions.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">general_sum_of_even_powers(eq,</span> <span class="pre">limit)</span></code> : Here <code class="docutils literal notranslate"><span class="pre">eq</span></code> is an expression which
is assumed to be zero. Also, <code class="docutils literal notranslate"><span class="pre">eq</span></code> should be in the form,
<span class="math notranslate nohighlight">\(x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_general_sum_of_even_powers</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_general_sum_of_even_powers</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">4</span> <span class="o">-</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">3</span><span class="o">**</span><span class="mi">4</span><span class="p">))</span>
<span class="go">{(2, 3)}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.power_representation" title="sympy.solvers.diophantine.diophantine.power_representation"><code class="xref py py-obj docutils literal notranslate"><span class="pre">power_representation</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="power-representation">
<h3>power_representation<a class="headerlink" href="#power-representation" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.power_representation">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">power_representation</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">zeros</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3781-L3889"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.power_representation" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a generator for finding k-tuples of integers,
<span class="math notranslate nohighlight">\((n_{1}, n_{2}, . . . n_{k})\)</span>, such that
<span class="math notranslate nohighlight">\(n = n_{1}^p + n_{2}^p + . . . n_{k}^p\)</span>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">power_representation(n,</span> <span class="pre">p,</span> <span class="pre">k,</span> <span class="pre">zeros)</span></code>: Represent non-negative number
<code class="docutils literal notranslate"><span class="pre">n</span></code> as a sum of <code class="docutils literal notranslate"><span class="pre">k</span></code> <code class="docutils literal notranslate"><span class="pre">p</span></code>th powers. If <code class="docutils literal notranslate"><span class="pre">zeros</span></code> is true, then the
solutions is allowed to contain zeros.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">power_representation</span>
</pre></div>
</div>
<p>Represent 1729 as a sum of two cubes:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">power_representation</span><span class="p">(</span><span class="mi">1729</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(9, 10)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(1, 12)</span>
</pre></div>
</div>
<p>If the flag <span class="math notranslate nohighlight">\(zeros\)</span> is True, the solution may contain tuples with
zeros; any such solutions will be generated after the solutions
without zeros:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">power_representation</span><span class="p">(</span><span class="mi">125</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">zeros</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[(5, 6, 8), (3, 4, 10), (0, 5, 10), (0, 2, 11)]</span>
</pre></div>
</div>
<p>For even <span class="math notranslate nohighlight">\(p\)</span> the <span class="math notranslate nohighlight">\(permute_sign\)</span> function can be used to get all
signed values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.iterables</span> <span class="kn">import</span> <span class="n">permute_signs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">permute_signs</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">)))</span>
<span class="go">[(1, 12), (-1, 12), (1, -12), (-1, -12)]</span>
</pre></div>
</div>
<p>All possible signed permutations can also be obtained:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.iterables</span> <span class="kn">import</span> <span class="n">signed_permutations</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">signed_permutations</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">)))</span>
<span class="go">[(1, 12), (-1, 12), (1, -12), (-1, -12), (12, 1), (-12, 1), (12, -1), (-12, -1)]</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="partition">
<h3>partition<a class="headerlink" href="#partition" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.partition">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">partition</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">zeros</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3550-L3604"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.partition" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a generator that can be used to generate partitions of an integer
<span class="math notranslate nohighlight">\(n\)</span>.</p>
<p class="rubric">Explanation</p>
<p>A partition of <span class="math notranslate nohighlight">\(n\)</span> is a set of positive integers which add up to <span class="math notranslate nohighlight">\(n\)</span>. For
example, partitions of 3 are 3, 1 + 2, 1 + 1 + 1. A partition is returned
as a tuple. If <code class="docutils literal notranslate"><span class="pre">k</span></code> equals None, then all possible partitions are returned
irrespective of their size, otherwise only the partitions of size <code class="docutils literal notranslate"><span class="pre">k</span></code> are
returned. If the <code class="docutils literal notranslate"><span class="pre">zero</span></code> parameter is set to True then a suitable
number of zeros are added at the end of every partition of size less than
<code class="docutils literal notranslate"><span class="pre">k</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">zero</span></code> parameter is considered only if <code class="docutils literal notranslate"><span class="pre">k</span></code> is not None. When the
partitions are over, the last <span class="math notranslate nohighlight">\(next()\)</span> call throws the <code class="docutils literal notranslate"><span class="pre">StopIteration</span></code>
exception, so this function should always be used inside a try - except
block.</p>
<p class="rubric">Details</p>
<p><code class="docutils literal notranslate"><span class="pre">partition(n,</span> <span class="pre">k)</span></code>: Here <code class="docutils literal notranslate"><span class="pre">n</span></code> is a positive integer and <code class="docutils literal notranslate"><span class="pre">k</span></code> is the size
of the partition which is also positive integer.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">partition</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(1, 1, 1, 1, 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(1, 1, 1, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">(1, 1, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">(1, 2, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">zeros</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">(0, 0, 5)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="sum-of-three-squares">
<h3>sum_of_three_squares<a class="headerlink" href="#sum-of-three-squares" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.sum_of_three_squares">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">sum_of_three_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3650-L3727"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.sum_of_three_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a 3-tuple <span class="math notranslate nohighlight">\((a, b, c)\)</span> such that <span class="math notranslate nohighlight">\(a^2 + b^2 + c^2 = n\)</span> and
<span class="math notranslate nohighlight">\(a, b, c \geq 0\)</span>.</p>
<p>Returns None if <span class="math notranslate nohighlight">\(n = 4^a(8m + 7)\)</span> for some <span class="math notranslate nohighlight">\(a, m \in Z\)</span>. See
<a class="reference internal" href="#r719" id="id23"><span>[R719]</span></a> for more details.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">sum_of_three_squares(n)</span></code>: Here <code class="docutils literal notranslate"><span class="pre">n</span></code> is a non-negative integer.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">sum_of_three_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_of_three_squares</span><span class="p">(</span><span class="mi">44542</span><span class="p">)</span>
<span class="go">(18, 37, 207)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.sum_of_squares" title="sympy.solvers.diophantine.diophantine.sum_of_squares"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sum_of_squares</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r719"><span class="brackets">R719</span><span class="fn-backref">(<a href="#id23">1</a>,<a href="#id24">2</a>)</span></dt>
<dd><p>Representing a number as a sum of three squares, [online],
Available: <a class="reference external" href="http://schorn.ch/lagrange.html">http://schorn.ch/lagrange.html</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="sum-of-four-squares">
<h3>sum_of_four_squares<a class="headerlink" href="#sum-of-four-squares" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.sum_of_four_squares">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">sum_of_four_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3730-L3778"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.sum_of_four_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a 4-tuple <span class="math notranslate nohighlight">\((a, b, c, d)\)</span> such that <span class="math notranslate nohighlight">\(a^2 + b^2 + c^2 + d^2 = n\)</span>.</p>
<p>Here <span class="math notranslate nohighlight">\(a, b, c, d \geq 0\)</span>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">sum_of_four_squares(n)</span></code>: Here <code class="docutils literal notranslate"><span class="pre">n</span></code> is a non-negative integer.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">sum_of_four_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_of_four_squares</span><span class="p">(</span><span class="mi">3456</span><span class="p">)</span>
<span class="go">(8, 8, 32, 48)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sum_of_four_squares</span><span class="p">(</span><span class="mi">1294585930293</span><span class="p">)</span>
<span class="go">(0, 1234, 2161, 1137796)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.sum_of_squares" title="sympy.solvers.diophantine.diophantine.sum_of_squares"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sum_of_squares</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r720"><span class="brackets"><a class="fn-backref" href="#id25">R720</a></span></dt>
<dd><p>Representing a number as a sum of four squares, [online],
Available: <a class="reference external" href="http://schorn.ch/lagrange.html">http://schorn.ch/lagrange.html</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="sum-of-powers">
<h3>sum_of_powers<a class="headerlink" href="#sum-of-powers" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.sum_of_powers">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">sum_of_powers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">zeros</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3781-L3889"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.sum_of_powers" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a generator for finding k-tuples of integers,
<span class="math notranslate nohighlight">\((n_{1}, n_{2}, . . . n_{k})\)</span>, such that
<span class="math notranslate nohighlight">\(n = n_{1}^p + n_{2}^p + . . . n_{k}^p\)</span>.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">power_representation(n,</span> <span class="pre">p,</span> <span class="pre">k,</span> <span class="pre">zeros)</span></code>: Represent non-negative number
<code class="docutils literal notranslate"><span class="pre">n</span></code> as a sum of <code class="docutils literal notranslate"><span class="pre">k</span></code> <code class="docutils literal notranslate"><span class="pre">p</span></code>th powers. If <code class="docutils literal notranslate"><span class="pre">zeros</span></code> is true, then the
solutions is allowed to contain zeros.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">power_representation</span>
</pre></div>
</div>
<p>Represent 1729 as a sum of two cubes:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">power_representation</span><span class="p">(</span><span class="mi">1729</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(9, 10)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">(1, 12)</span>
</pre></div>
</div>
<p>If the flag <span class="math notranslate nohighlight">\(zeros\)</span> is True, the solution may contain tuples with
zeros; any such solutions will be generated after the solutions
without zeros:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">power_representation</span><span class="p">(</span><span class="mi">125</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">zeros</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[(5, 6, 8), (3, 4, 10), (0, 5, 10), (0, 2, 11)]</span>
</pre></div>
</div>
<p>For even <span class="math notranslate nohighlight">\(p\)</span> the <span class="math notranslate nohighlight">\(permute_sign\)</span> function can be used to get all
signed values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.iterables</span> <span class="kn">import</span> <span class="n">permute_signs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">permute_signs</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">)))</span>
<span class="go">[(1, 12), (-1, 12), (1, -12), (-1, -12)]</span>
</pre></div>
</div>
<p>All possible signed permutations can also be obtained:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.iterables</span> <span class="kn">import</span> <span class="n">signed_permutations</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">signed_permutations</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">)))</span>
<span class="go">[(1, 12), (-1, 12), (1, -12), (-1, -12), (12, 1), (-12, 1), (12, -1), (-12, -1)]</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="sum-of-squares">
<h3>sum_of_squares<a class="headerlink" href="#sum-of-squares" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.sum_of_squares">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">sum_of_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">zeros</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3907-L3952"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.sum_of_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a generator that yields the k-tuples of nonnegative
values, the squares of which sum to n. If zeros is False (default)
then the solution will not contain zeros. The nonnegative
elements of a tuple are sorted.</p>
<ul class="simple">
<li><p>If k == 1 and n is square, (n,) is returned.</p></li>
<li><p>If k == 2 then n can only be written as a sum of squares if
every prime in the factorization of n that has the form
4*k + 3 has an even multiplicity. If n is prime then
it can only be written as a sum of two squares if it is
in the form 4*k + 1.</p></li>
<li><p>if k == 3 then n can be written as a sum of squares if it does
not have the form 4**m*(8*k + 7).</p></li>
<li><p>all integers can be written as the sum of 4 squares.</p></li>
<li><p>if k &gt; 4 then n can be partitioned and each partition can
be written as a sum of 4 squares; if n is not evenly divisible
by 4 then n can be written as a sum of squares only if the
an additional partition can be written as sum of squares.
For example, if k = 6 then n is partitioned into two parts,
the first being written as a sum of 4 squares and the second
being written as a sum of 2 squares – which can only be
done if the condition above for k = 2 can be met, so this will
automatically reject certain partitions of n.</p></li>
</ul>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">sum_of_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sum_of_squares</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">[(3, 4)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sum_of_squares</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
<span class="go">[(3, 4), (0, 5)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sum_of_squares</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">[(1, 2, 2, 4)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../utilities/iterables.html#sympy.utilities.iterables.signed_permutations" title="sympy.utilities.iterables.signed_permutations"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.utilities.iterables.signed_permutations</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="merge-solution">
<h3>merge_solution<a class="headerlink" href="#merge-solution" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.merge_solution">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">merge_solution</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">var</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">var_t</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">solution</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1535-L1567"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.merge_solution" title="Permalink to this definition">¶</a></dt>
<dd><p>This is used to construct the full solution from the solutions of sub
equations.</p>
<p class="rubric">Explanation</p>
<p>For example when solving the equation <span class="math notranslate nohighlight">\((x - y)(x^2 + y^2 - z^2) = 0\)</span>,
solutions for each of the equations <span class="math notranslate nohighlight">\(x - y = 0\)</span> and <span class="math notranslate nohighlight">\(x^2 + y^2 - z^2\)</span> are
found independently. Solutions for <span class="math notranslate nohighlight">\(x - y = 0\)</span> are <span class="math notranslate nohighlight">\((x, y) = (t, t)\)</span>. But
we should introduce a value for z when we output the solution for the
original equation. This function converts <span class="math notranslate nohighlight">\((t, t)\)</span> into <span class="math notranslate nohighlight">\((t, t, n_{1})\)</span>
where <span class="math notranslate nohighlight">\(n_{1}\)</span> is an integer parameter.</p>
</dd></dl>

</section>
<section id="divisible">
<h3>divisible<a class="headerlink" href="#divisible" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.divisible">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">divisible</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1884-L1888"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.divisible" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <span class="math notranslate nohighlight">\(True\)</span> if <code class="docutils literal notranslate"><span class="pre">a</span></code> is divisible by <code class="docutils literal notranslate"><span class="pre">b</span></code> and <span class="math notranslate nohighlight">\(False\)</span> otherwise.</p>
</dd></dl>

</section>
<section id="pqa">
<h3>PQa<a class="headerlink" href="#pqa" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.PQa">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">PQa</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">P_0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Q_0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">D</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2317-L2377"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.PQa" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns useful information needed to solve the Pell equation.</p>
<p class="rubric">Explanation</p>
<p>There are six sequences of integers defined related to the continued
fraction representation of <span class="math notranslate nohighlight">\(\\frac{P + \sqrt{D}}{Q}\)</span>, namely {<span class="math notranslate nohighlight">\(P_{i}\)</span>},
{<span class="math notranslate nohighlight">\(Q_{i}\)</span>}, {<span class="math notranslate nohighlight">\(a_{i}\)</span>},{<span class="math notranslate nohighlight">\(A_{i}\)</span>}, {<span class="math notranslate nohighlight">\(B_{i}\)</span>}, {<span class="math notranslate nohighlight">\(G_{i}\)</span>}. <code class="docutils literal notranslate"><span class="pre">PQa()</span></code> Returns
these values as a 6-tuple in the same order as mentioned above. Refer <a class="reference internal" href="#r721" id="id26"><span>[R721]</span></a>
for more detailed information.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">PQa(P_0,</span> <span class="pre">Q_0,</span> <span class="pre">D)</span></code>: <code class="docutils literal notranslate"><span class="pre">P_0</span></code>, <code class="docutils literal notranslate"><span class="pre">Q_0</span></code> and <code class="docutils literal notranslate"><span class="pre">D</span></code> are integers corresponding
to <span class="math notranslate nohighlight">\(P_{0}\)</span>, <span class="math notranslate nohighlight">\(Q_{0}\)</span> and <span class="math notranslate nohighlight">\(D\)</span> in the continued fraction
<span class="math notranslate nohighlight">\(\\frac{P_{0} + \sqrt{D}}{Q_{0}}\)</span>.
Also it’s assumed that <span class="math notranslate nohighlight">\(P_{0}^2 == D mod(|Q_{0}|)\)</span> and <span class="math notranslate nohighlight">\(D\)</span> is square free.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">PQa</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pqa</span> <span class="o">=</span> <span class="n">PQa</span><span class="p">(</span><span class="mi">13</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="c1"># (13 + sqrt(5))/4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">pqa</span><span class="p">)</span> <span class="c1"># (P_0, Q_0, a_0, A_0, B_0, G_0)</span>
<span class="go">(13, 4, 3, 3, 1, -1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">pqa</span><span class="p">)</span> <span class="c1"># (P_1, Q_1, a_1, A_1, B_1, G_1)</span>
<span class="go">(-1, 1, 1, 4, 1, 3)</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r721"><span class="brackets">R721</span><span class="fn-backref">(<a href="#id26">1</a>,<a href="#id27">2</a>)</span></dt>
<dd><p>Solving the generalized Pell equation x^2 - Dy^2 = N, John P.
Robertson, July 31, 2004, Pages 4 - 8. <a class="reference external" href="https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf">https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="equivalent">
<h3>equivalent<a class="headerlink" href="#equivalent" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.equivalent">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">equivalent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">u</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">r</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">D</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">N</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2471-L2507"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.equivalent" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if two solutions <span class="math notranslate nohighlight">\((u, v)\)</span> and <span class="math notranslate nohighlight">\((r, s)\)</span> of <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span>
belongs to the same equivalence class and False otherwise.</p>
<p class="rubric">Explanation</p>
<p>Two solutions <span class="math notranslate nohighlight">\((u, v)\)</span> and <span class="math notranslate nohighlight">\((r, s)\)</span> to the above equation fall to the same
equivalence class iff both <span class="math notranslate nohighlight">\((ur - Dvs)\)</span> and <span class="math notranslate nohighlight">\((us - vr)\)</span> are divisible by
<span class="math notranslate nohighlight">\(N\)</span>. See reference <a class="reference internal" href="#r722" id="id28"><span>[R722]</span></a>. No test is performed to test whether <span class="math notranslate nohighlight">\((u, v)\)</span> and
<span class="math notranslate nohighlight">\((r, s)\)</span> are actually solutions to the equation. User should take care of
this.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">equivalent(u,</span> <span class="pre">v,</span> <span class="pre">r,</span> <span class="pre">s,</span> <span class="pre">D,</span> <span class="pre">N)</span></code>: <span class="math notranslate nohighlight">\((u, v)\)</span> and <span class="math notranslate nohighlight">\((r, s)\)</span> are two solutions
of the equation <span class="math notranslate nohighlight">\(x^2 - Dy^2 = N\)</span> and all parameters involved are integers.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">equivalent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">equivalent</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">18</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">equivalent</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">18</span><span class="p">,</span> <span class="mi">393</span><span class="p">,</span> <span class="mi">109</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r722"><span class="brackets">R722</span><span class="fn-backref">(<a href="#id28">1</a>,<a href="#id29">2</a>)</span></dt>
<dd><p>Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
Robertson, July 31, 2004, Page 12. <a class="reference external" href="https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf">https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="parametrize-ternary-quadratic">
<h3>parametrize_ternary_quadratic<a class="headerlink" href="#parametrize-ternary-quadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.parametrize_ternary_quadratic">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">parametrize_ternary_quadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L2937-L2999"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.parametrize_ternary_quadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the parametrized general solution for the ternary quadratic
equation <code class="docutils literal notranslate"><span class="pre">eq</span></code> which has the form
<span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">ordered</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">parametrize_ternary_quadratic</span>
</pre></div>
</div>
<p>The parametrized solution may be returned with three parameters:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parametrize_ternary_quadratic</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(p**2 - 2*q**2, -2*p**2 + 4*p*q - 4*p*r - 4*q**2, p**2 - 4*p*q + 2*q**2 - 4*q*r)</span>
</pre></div>
</div>
<p>There might also be only two parameters:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parametrize_ternary_quadratic</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(2*p**2 - 3*q**2, -4*p**2 + 12*p*q - 6*q**2, 4*p**2 - 8*p*q + 6*q**2)</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>Consider <code class="docutils literal notranslate"><span class="pre">p</span></code> and <code class="docutils literal notranslate"><span class="pre">q</span></code> in the previous 2-parameter
solution and observe that more than one solution can be represented
by a given pair of parameters. If <span class="math notranslate nohighlight">\(p\)</span> and <code class="docutils literal notranslate"><span class="pre">q</span></code> are not coprime, this is
trivially true since the common factor will also be a common factor of the
solution values. But it may also be true even when <code class="docutils literal notranslate"><span class="pre">p</span></code> and
<code class="docutils literal notranslate"><span class="pre">q</span></code> are coprime:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sol</span> <span class="o">=</span> <span class="n">Tuple</span><span class="p">(</span><span class="o">*</span><span class="n">_</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">,</span> <span class="n">q</span> <span class="o">=</span> <span class="n">ordered</span><span class="p">(</span><span class="n">sol</span><span class="o">.</span><span class="n">free_symbols</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sol</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">p</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
<span class="go">(6, 12, 12)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sol</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">q</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="mi">1</span><span class="p">)])</span>
<span class="go">(-1, 2, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sol</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">q</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="mi">1</span><span class="p">)])</span>
<span class="go">(2, -4, 4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sol</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">q</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="mi">0</span><span class="p">)])</span>
<span class="go">(-3, -6, 6)</span>
</pre></div>
</div>
<p>Except for sign and a common factor, these are equivalent to
the solution of (1, 2, 2).</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r723"><span class="brackets"><a class="fn-backref" href="#id30">R723</a></span></dt>
<dd><p>The algorithmic resolution of Diophantine equations, Nigel P. Smart,
London Mathematical Society Student Texts 41, Cambridge University
Press, Cambridge, 1998.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="diop-ternary-quadratic-normal">
<h3>diop_ternary_quadratic_normal<a class="headerlink" href="#diop-ternary-quadratic-normal" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.diop_ternary_quadratic_normal">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">diop_ternary_quadratic_normal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameterize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3042-L3083"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.diop_ternary_quadratic_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Solves the quadratic ternary diophantine equation,
<span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 = 0\)</span>.</p>
<p class="rubric">Explanation</p>
<p>Here the coefficients <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span>, and <span class="math notranslate nohighlight">\(c\)</span> should be non zero. Otherwise the
equation will be a quadratic binary or univariate equation. If solvable,
returns a tuple <span class="math notranslate nohighlight">\((x, y, z)\)</span> that satisfies the given equation. If the
equation does not have integer solutions, <span class="math notranslate nohighlight">\((None, None, None)\)</span> is returned.</p>
<p class="rubric">Usage</p>
<p><code class="docutils literal notranslate"><span class="pre">diop_ternary_quadratic_normal(eq)</span></code>: where <code class="docutils literal notranslate"><span class="pre">eq</span></code> is an equation of the form
<span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 = 0\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">diop_ternary_quadratic_normal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic_normal</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(1, 0, 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic_normal</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(1, 0, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diop_ternary_quadratic_normal</span><span class="p">(</span><span class="mi">34</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">301</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(4, 9, 1)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="ldescent">
<h3>ldescent<a class="headerlink" href="#ldescent" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.ldescent">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">ldescent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3189-L3255"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.ldescent" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a non-trivial solution to <span class="math notranslate nohighlight">\(w^2 = Ax^2 + By^2\)</span> using
Lagrange’s method; return None if there is no such solution.
.</p>
<p>Here, <span class="math notranslate nohighlight">\(A \neq 0\)</span> and <span class="math notranslate nohighlight">\(B \neq 0\)</span> and <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span> are square free. Output a
tuple <span class="math notranslate nohighlight">\((w_0, x_0, y_0)\)</span> which is a solution to the above equation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">ldescent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ldescent</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># w^2 = x^2 + y^2</span>
<span class="go">(1, 1, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ldescent</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="c1"># w^2 = 4x^2 - 7y^2</span>
<span class="go">(2, -1, 0)</span>
</pre></div>
</div>
<p>This means that <span class="math notranslate nohighlight">\(x = -1, y = 0\)</span> and <span class="math notranslate nohighlight">\(w = 2\)</span> is a solution to the equation
<span class="math notranslate nohighlight">\(w^2 = 4x^2 - 7y^2\)</span></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ldescent</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># w^2 = 5x^2 - y^2</span>
<span class="go">(2, 1, -1)</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r724"><span class="brackets"><a class="fn-backref" href="#id31">R724</a></span></dt>
<dd><p>The algorithmic resolution of Diophantine equations, Nigel P. Smart,
London Mathematical Society Student Texts 41, Cambridge University
Press, Cambridge, 1998.</p>
</dd>
<dt class="label" id="r725"><span class="brackets"><a class="fn-backref" href="#id32">R725</a></span></dt>
<dd><p>Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
[online], Available:
<a class="reference external" href="http://eprints.nottingham.ac.uk/60/1/kvxefz87.pdf">http://eprints.nottingham.ac.uk/60/1/kvxefz87.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="gaussian-reduce">
<h3>gaussian_reduce<a class="headerlink" href="#gaussian-reduce" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.gaussian_reduce">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">gaussian_reduce</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">w</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3311-L3349"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.gaussian_reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a reduced solution <span class="math notranslate nohighlight">\((x, z)\)</span> to the congruence
<span class="math notranslate nohighlight">\(X^2 - aZ^2 \equiv 0 \ (mod \ b)\)</span> so that <span class="math notranslate nohighlight">\(x^2 + |a|z^2\)</span> is minimal.</p>
<p class="rubric">Details</p>
<p>Here <code class="docutils literal notranslate"><span class="pre">w</span></code> is a solution of the congruence <span class="math notranslate nohighlight">\(x^2 \equiv a \ (mod \ b)\)</span></p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r726"><span class="brackets"><a class="fn-backref" href="#id33">R726</a></span></dt>
<dd><p>Gaussian lattice Reduction [online]. Available:
<a class="reference external" href="http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404">http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404</a></p>
</dd>
<dt class="label" id="r727"><span class="brackets"><a class="fn-backref" href="#id34">R727</a></span></dt>
<dd><p>Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
Mathematics of Computation, Volume 00, Number 0.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="holzer">
<h3>holzer<a class="headerlink" href="#holzer" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.holzer">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">holzer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">z</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">c</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3373-L3434"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.holzer" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplify the solution <span class="math notranslate nohighlight">\((x, y, z)\)</span> of the equation
<span class="math notranslate nohighlight">\(ax^2 + by^2 = cz^2\)</span> with <span class="math notranslate nohighlight">\(a, b, c &gt; 0\)</span> and <span class="math notranslate nohighlight">\(z^2 \geq \mid ab \mid\)</span> to
a new reduced solution <span class="math notranslate nohighlight">\((x', y', z')\)</span> such that <span class="math notranslate nohighlight">\(z'^2 \leq \mid ab \mid\)</span>.</p>
<p>The algorithm is an interpretation of Mordell’s reduction as described
on page 8 of Cremona and Rusin’s paper <a class="reference internal" href="#r728" id="id35"><span>[R728]</span></a> and the work of Mordell in
reference <a class="reference internal" href="#r729" id="id36"><span>[R729]</span></a>.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r728"><span class="brackets">R728</span><span class="fn-backref">(<a href="#id35">1</a>,<a href="#id37">2</a>)</span></dt>
<dd><p>Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
Mathematics of Computation, Volume 00, Number 0.</p>
</dd>
<dt class="label" id="r729"><span class="brackets">R729</span><span class="fn-backref">(<a href="#id36">1</a>,<a href="#id38">2</a>)</span></dt>
<dd><p>Diophantine Equations, L. J. Mordell, page 48.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="prime-as-sum-of-two-squares">
<h3>prime_as_sum_of_two_squares<a class="headerlink" href="#prime-as-sum-of-two-squares" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.prime_as_sum_of_two_squares">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">prime_as_sum_of_two_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3607-L3647"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.prime_as_sum_of_two_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Represent a prime <span class="math notranslate nohighlight">\(p\)</span> as a unique sum of two squares; this can
only be done if the prime is congruent to 1 mod 4.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">prime_as_sum_of_two_squares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prime_as_sum_of_two_squares</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>  <span class="c1"># can&#39;t be done</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prime_as_sum_of_two_squares</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="go">(1, 2)</span>
</pre></div>
</div>
<p class="rubric">Reference</p>
<dl class="citation">
<dt class="label" id="r730"><span class="brackets">R730</span></dt>
<dd><p>Representing a number as a sum of four squares, [online],
Available: <a class="reference external" href="http://schorn.ch/lagrange.html">http://schorn.ch/lagrange.html</a></p>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.sum_of_squares" title="sympy.solvers.diophantine.diophantine.sum_of_squares"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sum_of_squares</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="sqf-normal">
<h3>sqf_normal<a class="headerlink" href="#sqf-normal" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.sqf_normal">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">sqf_normal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">c</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">steps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3091-L3145"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.sqf_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <span class="math notranslate nohighlight">\(a', b', c'\)</span>, the coefficients of the square-free normal
form of <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 = 0\)</span>, where <span class="math notranslate nohighlight">\(a', b', c'\)</span> are pairwise
prime.  If <span class="math notranslate nohighlight">\(steps\)</span> is True then also return three tuples:
<span class="math notranslate nohighlight">\(sq\)</span>, <span class="math notranslate nohighlight">\(sqf\)</span>, and <span class="math notranslate nohighlight">\((a', b', c')\)</span> where <span class="math notranslate nohighlight">\(sq\)</span> contains the square
factors of <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span> and <span class="math notranslate nohighlight">\(c\)</span> after removing the <span class="math notranslate nohighlight">\(gcd(a, b, c)\)</span>;
<span class="math notranslate nohighlight">\(sqf\)</span> contains the values of <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span> and <span class="math notranslate nohighlight">\(c\)</span> after removing
both the <span class="math notranslate nohighlight">\(gcd(a, b, c)\)</span> and the square factors.</p>
<p>The solutions for <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2 = 0\)</span> can be
recovered from the solutions of <span class="math notranslate nohighlight">\(a'x^2 + b'y^2 + c'z^2 = 0\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">sqf_normal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqf_normal</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">3</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">7</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">11</span><span class="p">)</span>
<span class="go">(11, 1, 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqf_normal</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">3</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">7</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">11</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="go">((3, 1, 7), (5, 55, 11), (11, 1, 5))</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.solvers.diophantine.diophantine.reconstruct" title="sympy.solvers.diophantine.diophantine.reconstruct"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reconstruct</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r731"><span class="brackets"><a class="fn-backref" href="#id39">R731</a></span></dt>
<dd><p>Legendre’s Theorem, Legrange’s Descent,
<a class="reference external" href="http://public.csusm.edu/aitken_html/notes/legendre.pdf">http://public.csusm.edu/aitken_html/notes/legendre.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="reconstruct">
<h3>reconstruct<a class="headerlink" href="#reconstruct" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.reconstruct">
<span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">reconstruct</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">z</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L3174-L3186"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.reconstruct" title="Permalink to this definition">¶</a></dt>
<dd><p>Reconstruct the <span class="math notranslate nohighlight">\(z\)</span> value of an equivalent solution of <span class="math notranslate nohighlight">\(ax^2 + by^2 + cz^2\)</span>
from the <span class="math notranslate nohighlight">\(z\)</span> value of a solution of the square-free normal form of the
equation, <span class="math notranslate nohighlight">\(a'*x^2 + b'*y^2 + c'*z^2\)</span>, where <span class="math notranslate nohighlight">\(a'\)</span>, <span class="math notranslate nohighlight">\(b'\)</span> and <span class="math notranslate nohighlight">\(c'\)</span> are square
free and <span class="math notranslate nohighlight">\(gcd(a', b', c') == 1\)</span>.</p>
</dd></dl>

</section>
</section>
<section id="internal-classes">
<h2>Internal Classes<a class="headerlink" href="#internal-classes" title="Permalink to this headline">¶</a></h2>
<p>These classes are intended for internal use in the Diophantine module.</p>
<section id="diophantinesolutionset">
<h3>DiophantineSolutionSet<a class="headerlink" href="#diophantinesolutionset" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.DiophantineSolutionSet">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">DiophantineSolutionSet</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">symbols_seq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameters</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L36-L132"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.DiophantineSolutionSet" title="Permalink to this definition">¶</a></dt>
<dd><p>Container for a set of solutions to a particular diophantine equation.</p>
<p>The base representation is a set of tuples representing each of the solutions.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>symbols</strong> : list</p>
<blockquote>
<div><p>List of free symbols in the original equation.</p>
</div></blockquote>
<p><strong>parameters: list</strong></p>
<blockquote>
<div><p>List of parameters to be used in the solution.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Adding solutions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">DiophantineSolutionSet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">u</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span> <span class="o">=</span> <span class="n">DiophantineSolutionSet</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">],</span> <span class="p">[</span><span class="n">t</span><span class="p">,</span> <span class="n">u</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span>
<span class="go">set()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">u</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span>
<span class="go">{(-1, u), (2, 3)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s2</span> <span class="o">=</span> <span class="n">DiophantineSolutionSet</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">],</span> <span class="p">[</span><span class="n">t</span><span class="p">,</span> <span class="n">u</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s2</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="o">*</span><span class="n">s2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span>
<span class="go">{(-1, u), (2, 3), (3, 4)}</span>
</pre></div>
</div>
<p>Conversion of solutions into dicts:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">dict_iterator</span><span class="p">())</span>
<span class="go">[{x: -1, y: u}, {x: 2, y: 3}, {x: 3, y: 4}]</span>
</pre></div>
</div>
<p>Substituting values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span> <span class="o">=</span> <span class="n">DiophantineSolutionSet</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">],</span> <span class="p">[</span><span class="n">t</span><span class="p">,</span> <span class="n">u</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">t</span> <span class="o">+</span> <span class="n">u</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span>
<span class="go">{(t**2, t + u)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="o">.</span><span class="n">subs</span><span class="p">({</span><span class="n">t</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="n">u</span><span class="p">:</span> <span class="mi">3</span><span class="p">})</span>
<span class="go">{(4, 5)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">{(1, u - 1)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">{(9, u + 3)}</span>
</pre></div>
</div>
<p>Evaluation at specific values. Positional arguments are given in the same order as the parameters:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">{(4, 1)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="go">{(25, u + 5)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">{(t**2, t + 2)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="diophantineequationtype">
<h3>DiophantineEquationType<a class="headerlink" href="#diophantineequationtype" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.DiophantineEquationType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">DiophantineEquationType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L135-L210"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.DiophantineEquationType" title="Permalink to this definition">¶</a></dt>
<dd><p>Internal representation of a particular diophantine equation type.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>equation :</strong></p>
<blockquote>
<div><p>The diophantine equation that is being solved.</p>
</div></blockquote>
<p><strong>free_symbols</strong> : list (optional)</p>
<blockquote>
<div><p>The symbols being solved for.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Attributes</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 19%" />
<col style="width: 81%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>total_degree :</p></td>
<td><p>The maximum of the degrees of all terms in the equation</p></td>
</tr>
<tr class="row-even"><td><p>homogeneous :</p></td>
<td><p>Does the equation contain a term of degree 0</p></td>
</tr>
<tr class="row-odd"><td><p>homogeneous_order :</p></td>
<td><p>Does the equation contain any coefficient that is in the symbols being solved for</p></td>
</tr>
<tr class="row-even"><td><p>dimension :</p></td>
<td><p>The number of symbols being solved for</p></td>
</tr>
</tbody>
</table>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.DiophantineEquationType.matches">
<span class="sig-name descname"><span class="pre">matches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L183-L187"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.DiophantineEquationType.matches" title="Permalink to this definition">¶</a></dt>
<dd><p>Determine whether the given equation can be matched to the particular equation type.</p>
</dd></dl>

</dd></dl>

</section>
<section id="univariate">
<h3>Univariate<a class="headerlink" href="#univariate" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.Univariate">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">Univariate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L213-L242"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.Univariate" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a univariate diophantine equation.</p>
<p>A univariate diophantine equation is an equation of the form
<span class="math notranslate nohighlight">\(a_{0} + a_{1}x + a_{2}x^2 + .. + a_{n}x^n = 0\)</span> where <span class="math notranslate nohighlight">\(a_{1}, a_{2}, ..a_{n}\)</span> are
integer constants and <span class="math notranslate nohighlight">\(x\)</span> is an integer variable.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">Univariate</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Univariate</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span> <span class="c1"># solves equation (x - 2)*(x - 3)**2 == 0</span>
<span class="go">{(2,), (3,)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="linear">
<h3>Linear<a class="headerlink" href="#linear" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.Linear">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">Linear</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L245-L442"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.Linear" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a linear diophantine equation.</p>
<p>A linear diophantine equation is an equation of the form <span class="math notranslate nohighlight">\(a_{1}x_{1} +
a_{2}x_{2} + .. + a_{n}x_{n} = 0\)</span> where <span class="math notranslate nohighlight">\(a_{1}, a_{2}, ..a_{n}\)</span> are
integer constants and <span class="math notranslate nohighlight">\(x_{1}, x_{2}, ..x_{n}\)</span> are integer variables.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">Linear</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l1</span> <span class="o">=</span> <span class="n">Linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l1</span><span class="o">.</span><span class="n">matches</span><span class="p">()</span> <span class="c1"># is this equation linear</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l1</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span> <span class="c1"># solves equation 2*x - 3*y - 5 == 0</span>
<span class="go">{(3*t_0 - 5, 2*t_0 - 5)}</span>
</pre></div>
</div>
<p>Here x = -3*t_0 - 5 and y = -2*t_0 - 5</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Linear</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">z</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="binaryquadratic">
<h3>BinaryQuadratic<a class="headerlink" href="#binaryquadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.BinaryQuadratic">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">BinaryQuadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L445-L665"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.BinaryQuadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a binary quadratic diophantine equation.</p>
<p>A binary quadratic diophantine equation is an equation of the
form <span class="math notranslate nohighlight">\(Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0\)</span>, where <span class="math notranslate nohighlight">\(A, B, C, D, E,
F\)</span> are integer constants and <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> are integer variables.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">BinaryQuadratic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b1</span> <span class="o">=</span> <span class="n">BinaryQuadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b1</span><span class="o">.</span><span class="n">matches</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b2</span> <span class="o">=</span> <span class="n">BinaryQuadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b2</span><span class="o">.</span><span class="n">matches</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b2</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(-1, -1)}</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r732"><span class="brackets"><a class="fn-backref" href="#id40">R732</a></span></dt>
<dd><p>Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online],
Available: <a class="reference external" href="http://www.alpertron.com.ar/METHODS.HTM">http://www.alpertron.com.ar/METHODS.HTM</a></p>
</dd>
<dt class="label" id="r733"><span class="brackets"><a class="fn-backref" href="#id41">R733</a></span></dt>
<dd><p>Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online],
Available: <a class="reference external" href="https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf">https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="inhomogeneousternaryquadratic">
<h3>InhomogeneousTernaryQuadratic<a class="headerlink" href="#inhomogeneousternaryquadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.InhomogeneousTernaryQuadratic">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">InhomogeneousTernaryQuadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L668-L684"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.InhomogeneousTernaryQuadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of an inhomogeneous ternary quadratic.</p>
<p>No solver is currently implemented for this equation type.</p>
</dd></dl>

</section>
<section id="homogeneousternaryquadraticnormal">
<h3>HomogeneousTernaryQuadraticNormal<a class="headerlink" href="#homogeneousternaryquadraticnormal" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadraticNormal">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">HomogeneousTernaryQuadraticNormal</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L687-L771"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadraticNormal" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a homogeneous ternary quadratic normal diophantine equation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">HomogeneousTernaryQuadraticNormal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">HomogeneousTernaryQuadraticNormal</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">5</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(1, 2, 4)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="homogeneousternaryquadratic">
<h3>HomogeneousTernaryQuadratic<a class="headerlink" href="#homogeneousternaryquadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadratic">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">HomogeneousTernaryQuadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L774-L916"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a homogeneous ternary quadratic diophantine equation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">HomogeneousTernaryQuadratic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">HomogeneousTernaryQuadratic</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(-1, 2, 1)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">HomogeneousTernaryQuadratic</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">z</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(3, 12, 13)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="inhomogeneousgeneralquadratic">
<h3>InhomogeneousGeneralQuadratic<a class="headerlink" href="#inhomogeneousgeneralquadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.InhomogeneousGeneralQuadratic">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">InhomogeneousGeneralQuadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L919-L939"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.InhomogeneousGeneralQuadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of an inhomogeneous general quadratic.</p>
<p>No solver is currently implemented for this equation type.</p>
</dd></dl>

</section>
<section id="homogeneousgeneralquadratic">
<h3>HomogeneousGeneralQuadratic<a class="headerlink" href="#homogeneousgeneralquadratic" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.HomogeneousGeneralQuadratic">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">HomogeneousGeneralQuadratic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L942-L962"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.HomogeneousGeneralQuadratic" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a homogeneous general quadratic.</p>
<p>No solver is currently implemented for this equation type.</p>
</dd></dl>

</section>
<section id="generalsumofsquares">
<h3>GeneralSumOfSquares<a class="headerlink" href="#generalsumofsquares" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.GeneralSumOfSquares">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">GeneralSumOfSquares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L965-L1033"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.GeneralSumOfSquares" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of the diophantine equation</p>
<p><span class="math notranslate nohighlight">\(x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0\)</span>.</p>
<p class="rubric">Details</p>
<p>When <span class="math notranslate nohighlight">\(n = 3\)</span> if <span class="math notranslate nohighlight">\(k = 4^a(8m + 7)\)</span> for some <span class="math notranslate nohighlight">\(a, m \in Z\)</span> then there will be
no solutions. Refer <a class="reference internal" href="#r734" id="id42"><span>[R734]</span></a> for more details.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">GeneralSumOfSquares</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GeneralSumOfSquares</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2345</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(15, 22, 22, 24, 24)}</span>
</pre></div>
</div>
<p>By default only 1 solution is returned. Use the <span class="math notranslate nohighlight">\(limit\)</span> keyword for more:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">GeneralSumOfSquares</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2345</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">limit</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>
<span class="go">[(15, 22, 22, 24, 24), (16, 19, 24, 24, 24), (16, 20, 22, 23, 26)]</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r734"><span class="brackets">R734</span><span class="fn-backref">(<a href="#id42">1</a>,<a href="#id43">2</a>)</span></dt>
<dd><p>Representing an integer as a sum of three squares, [online],
Available:
<a class="reference external" href="http://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares">http://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="generalpythagorean">
<h3>GeneralPythagorean<a class="headerlink" href="#generalpythagorean" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.GeneralPythagorean">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">GeneralPythagorean</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1036-L1111"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.GeneralPythagorean" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of the general pythagorean equation,
<span class="math notranslate nohighlight">\(a_{1}^2x_{1}^2 + a_{2}^2x_{2}^2 + . . . + a_{n}^2x_{n}^2 - a_{n + 1}^2x_{n + 1}^2 = 0\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">GeneralPythagorean</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GeneralPythagorean</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">d</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(t_0**2 + t_1**2 - t_2**2, 2*t_0*t_2, 2*t_1*t_2, t_0**2 + t_1**2 + t_2**2)}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GeneralPythagorean</span><span class="p">(</span><span class="mi">9</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">16</span><span class="o">*</span><span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">25</span><span class="o">*</span><span class="n">d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">parameters</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span><span class="p">])</span>
<span class="go">{(-10*t**2 + 10*x**2 + 10*y**2 + 10*z**2, 15*t**2 + 15*x**2 + 15*y**2 + 15*z**2, 15*t*x, 12*t*y, 60*t*z)}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="cubicthue">
<h3>CubicThue<a class="headerlink" href="#cubicthue" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.CubicThue">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">CubicThue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1114-L1138"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.CubicThue" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of a cubic Thue diophantine equation.</p>
<p>A cubic Thue diophantine equation is a polynomial of the form
<span class="math notranslate nohighlight">\(f(x, y) = r\)</span> of degree 3, where <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> are integers
and <span class="math notranslate nohighlight">\(r\)</span> is a rational number.</p>
<p>No solver is currently implemented for this equation type.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">CubicThue</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span> <span class="o">=</span> <span class="n">CubicThue</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span><span class="o">.</span><span class="n">matches</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="generalsumofevenpowers">
<h3>GeneralSumOfEvenPowers<a class="headerlink" href="#generalsumofevenpowers" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.solvers.diophantine.diophantine.GeneralSumOfEvenPowers">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.solvers.diophantine.diophantine.</span></span><span class="sig-name descname"><span class="pre">GeneralSumOfEvenPowers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">equation</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">free_symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/solvers/diophantine/diophantine.py#L1141-L1201"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.solvers.diophantine.diophantine.GeneralSumOfEvenPowers" title="Permalink to this definition">¶</a></dt>
<dd><p>Representation of the diophantine equation</p>
<p><span class="math notranslate nohighlight">\(x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0\)</span></p>
<p>where <span class="math notranslate nohighlight">\(e\)</span> is an even, integer power.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.solvers.diophantine.diophantine</span> <span class="kn">import</span> <span class="n">GeneralSumOfEvenPowers</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GeneralSumOfEvenPowers</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">4</span> <span class="o">-</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">3</span><span class="o">**</span><span class="mi">4</span><span class="p">))</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="go">{(2, 3)}</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Diophantine</a><ul>
<li><a class="reference internal" href="#diophantine-equations">Diophantine equations</a></li>
<li><a class="reference internal" href="#module-structure">Module structure</a></li>
<li><a class="reference internal" href="#tutorial">Tutorial</a></li>
<li><a class="reference internal" href="#references">References</a></li>
<li><a class="reference internal" href="#user-functions">User Functions</a><ul>
<li><a class="reference internal" href="#id9">diophantine</a></li>
<li><a class="reference internal" href="#classify-diop">classify_diop</a></li>
</ul>
</li>
<li><a class="reference internal" href="#internal-functions">Internal Functions</a><ul>
<li><a class="reference internal" href="#diop-solve">diop_solve</a></li>
<li><a class="reference internal" href="#diop-linear">diop_linear</a></li>
<li><a class="reference internal" href="#base-solution-linear">base_solution_linear</a></li>
<li><a class="reference internal" href="#diop-quadratic">diop_quadratic</a></li>
<li><a class="reference internal" href="#diop-dn">diop_DN</a></li>
<li><a class="reference internal" href="#cornacchia">cornacchia</a></li>
<li><a class="reference internal" href="#diop-bf-dn">diop_bf_DN</a></li>
<li><a class="reference internal" href="#transformation-to-dn">transformation_to_DN</a></li>
<li><a class="reference internal" href="#transformation-to-normal">transformation_to_normal</a></li>
<li><a class="reference internal" href="#find-dn">find_DN</a></li>
<li><a class="reference internal" href="#diop-ternary-quadratic">diop_ternary_quadratic</a></li>
<li><a class="reference internal" href="#square-factor">square_factor</a></li>
<li><a class="reference internal" href="#descent">descent</a></li>
<li><a class="reference internal" href="#diop-general-pythagorean">diop_general_pythagorean</a></li>
<li><a class="reference internal" href="#diop-general-sum-of-squares">diop_general_sum_of_squares</a></li>
<li><a class="reference internal" href="#diop-general-sum-of-even-powers">diop_general_sum_of_even_powers</a></li>
<li><a class="reference internal" href="#power-representation">power_representation</a></li>
<li><a class="reference internal" href="#partition">partition</a></li>
<li><a class="reference internal" href="#sum-of-three-squares">sum_of_three_squares</a></li>
<li><a class="reference internal" href="#sum-of-four-squares">sum_of_four_squares</a></li>
<li><a class="reference internal" href="#sum-of-powers">sum_of_powers</a></li>
<li><a class="reference internal" href="#sum-of-squares">sum_of_squares</a></li>
<li><a class="reference internal" href="#merge-solution">merge_solution</a></li>
<li><a class="reference internal" href="#divisible">divisible</a></li>
<li><a class="reference internal" href="#pqa">PQa</a></li>
<li><a class="reference internal" href="#equivalent">equivalent</a></li>
<li><a class="reference internal" href="#parametrize-ternary-quadratic">parametrize_ternary_quadratic</a></li>
<li><a class="reference internal" href="#diop-ternary-quadratic-normal">diop_ternary_quadratic_normal</a></li>
<li><a class="reference internal" href="#ldescent">ldescent</a></li>
<li><a class="reference internal" href="#gaussian-reduce">gaussian_reduce</a></li>
<li><a class="reference internal" href="#holzer">holzer</a></li>
<li><a class="reference internal" href="#prime-as-sum-of-two-squares">prime_as_sum_of_two_squares</a></li>
<li><a class="reference internal" href="#sqf-normal">sqf_normal</a></li>
<li><a class="reference internal" href="#reconstruct">reconstruct</a></li>
</ul>
</li>
<li><a class="reference internal" href="#internal-classes">Internal Classes</a><ul>
<li><a class="reference internal" href="#diophantinesolutionset">DiophantineSolutionSet</a></li>
<li><a class="reference internal" href="#diophantineequationtype">DiophantineEquationType</a></li>
<li><a class="reference internal" href="#univariate">Univariate</a></li>
<li><a class="reference internal" href="#linear">Linear</a></li>
<li><a class="reference internal" href="#binaryquadratic">BinaryQuadratic</a></li>
<li><a class="reference internal" href="#inhomogeneousternaryquadratic">InhomogeneousTernaryQuadratic</a></li>
<li><a class="reference internal" href="#homogeneousternaryquadraticnormal">HomogeneousTernaryQuadraticNormal</a></li>
<li><a class="reference internal" href="#homogeneousternaryquadratic">HomogeneousTernaryQuadratic</a></li>
<li><a class="reference internal" href="#inhomogeneousgeneralquadratic">InhomogeneousGeneralQuadratic</a></li>
<li><a class="reference internal" href="#homogeneousgeneralquadratic">HomogeneousGeneralQuadratic</a></li>
<li><a class="reference internal" href="#generalsumofsquares">GeneralSumOfSquares</a></li>
<li><a class="reference internal" href="#generalpythagorean">GeneralPythagorean</a></li>
<li><a class="reference internal" href="#cubicthue">CubicThue</a></li>
<li><a class="reference internal" href="#generalsumofevenpowers">GeneralSumOfEvenPowers</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../diffgeom.html"
                        title="previous chapter">Differential Geometry</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../discrete.html"
                        title="next chapter">Discrete</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/solvers/diophantine.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../discrete.html" title="Discrete"
             >next</a> |</li>
        <li class="right" >
          <a href="../diffgeom.html" title="Differential Geometry"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Diophantine</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/solvers/diophantine.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:22 GMT -->
</html>